#!/bin/bash

# 初始化基础信息
function init(){
	if [ -d "www" ];then
		rm -rf www
	fi

	if [ -d "wwwlogs" ];then
		rm -rf wwwlogs
	fi

	mkdir -p www/default
	mkdir -p www/dockertest
	mkdir -p wwwlogs

	echo "<?php echo phpinfo();exit();" >> www/default/index.php
	echo "<?php echo phpinfo();exit();" >> www/dockertest/index.php
}

# 获取yam中所有的services参数
function get_services(){
	array=()
    for line in `cat $1`
    do
        if [ $(echo $line | grep "^nginx-.*:$") ]; then
            array[${#array[@]}]=${line%?}
        elif [ $(echo $line | grep "^phpfpm-.*:$") ]; then
        	array[${#array[@]}]=${line%?}
        elif [ $(echo $line | grep "^mysql-.*:$") ]; then
        	array[${#array[@]}]=${line%?}
        fi
    done
    echo ${array[@]}
}

# 初始化nginx fpm配置信息
function write_nginx_fpm(){
	if [ $2 == 'phpfpm-7.2' ]; then
		fpm_name=php72.fpm
		php_link='phpfpm72'
	elif [ $2 == 'phpfpm-7.3' ]; then
		fpm_name=php73.fpm
		php_link='phpfpm73'
	elif [ $2 == 'phpfpm-7.4' ]; then
		fpm_name=php74.fpm
		php_link='phpfpm74'
	elif [ $2 == 'phpfpm-8.0' ]; then
		fpm_name=php80.fpm
		php_link='phpfpm80'
	elif [ $2 == 'phpfpm-8.1' ]; then
		fpm_name=php81.fpm
		php_link='phpfpm81'
	fi

	# 写入php72.fpm
	echo "# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000" >> $1/conf.d/$fpm_name
	echo "#" >> $1/conf.d/$fpm_name
	echo "location ~ \.php$ {" >> $1/conf.d/$fpm_name
	echo "    fastcgi_pass   $php_link:9000;" >> $1/conf.d/$fpm_name
	echo "    fastcgi_index  index.php;" >> $1/conf.d/$fpm_name
	echo "    fastcgi_param  SCRIPT_FILENAME \$document_root\$fastcgi_script_name;" >> $1/conf.d/$fpm_name
	echo "    include        fastcgi_params;" >> $1/conf.d/$fpm_name
	echo "}" >> $1/conf.d/$fpm_name

	echo $fpm_name
}

# 初始化nginx conf配置信息
function write_nginx_conf(){
	# 写入nginx.conf文件
	echo "user nginx;" >> $1/nginx.conf
	echo "worker_processes auto;" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "error_log /var/log/nginx/error.log notice;" >> $1/nginx.conf
	echo "pid /var/run/nginx.pid;" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "events {" >> $1/nginx.conf
	echo "    worker_connections 1024;" >> $1/nginx.conf
	echo "}" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "http {" >> $1/nginx.conf
	echo "    include /etc/nginx/mime.types;" >> $1/nginx.conf
	echo "    default_type application/octet-stream;" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "    log_format main '\$remote_addr - \$remote_user [\$time_local] \"\$request\" '" >> $1/nginx.conf
	echo "                    '\$status \$body_bytes_sent \"\$http_referer\" '" >> $1/nginx.conf
	echo "                    '\"\$http_user_agent\" \"\$http_x_forwarded_for\"';" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "    access_log /wwwlogs/access.log;" >> $1/nginx.conf
	echo "    error_log /wwwlogs/error.log;" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "    sendfile on;" >> $1/nginx.conf
	echo "    #tcp_nopush on;" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "    keepalive_timeout 65;" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "    #gzip on;" >> $1/nginx.conf
	echo "" >> $1/nginx.conf
	echo "    include /etc/nginx/conf.d/*.conf;" >> $1/nginx.conf
	echo "}" >> $1/nginx.conf

	# 写入default.conf文件
	echo "server {" >> $1/conf.d/default.conf
	echo "    listen 80;" >> $1/conf.d/default.conf
	echo "    listen [::]:80;" >> $1/conf.d/default.conf
	echo "    server_name localhost;" >> $1/conf.d/default.conf
	echo "    root /www/default;" >> $1/conf.d/default.conf
	echo "" >> $1/conf.d/default.conf
	echo "    access_log /wwwlogs/default_access.log;" >> $1/conf.d/default.conf
	echo "    error_log /wwwlogs/default_error.log;" >> $1/conf.d/default.conf
	echo "" >> $1/conf.d/default.conf
	echo "    location / {" >> $1/conf.d/default.conf
	echo "        index index.html index.htm index.php;" >> $1/conf.d/default.conf
	echo "        try_files \$uri \$uri/ /index.php?\$query_string;" >> $1/conf.d/default.conf
	echo "    }" >> $1/conf.d/default.conf
	if [ "$2" != "" ]; then
		echo "" >> $1/conf.d/default.conf
		echo "    include conf.d/$2;" >> $1/conf.d/default.conf
	fi
	echo "}" >> $1/conf.d/default.conf

	# 写入www.dockertest.com
	echo "server {" >> $1/conf.d/www.dockertest.com.conf
	echo "    server_name www.dockertest.com;" >> $1/conf.d/www.dockertest.com.conf
	echo "    root /www/dockertest;" >> $1/conf.d/www.dockertest.com.conf
	echo "" >> $1/conf.d/www.dockertest.com.conf
	echo "    access_log /wwwlogs/dockertest_access.log;" >> $1/conf.d/www.dockertest.com.conf
	echo "    error_log /wwwlogs/dockertest_access.log;" >> $1/conf.d/www.dockertest.com.conf
	echo "" >> $1/conf.d/www.dockertest.com.conf
	echo "    location / {" >> $1/conf.d/www.dockertest.com.conf
	echo "        index index.html index.htm index.php;" >> $1/conf.d/www.dockertest.com.conf
	echo "        try_files \$uri \$uri/ /index.php?\$query_string;" >> $1/conf.d/www.dockertest.com.conf
	echo "    }" >> $1/conf.d/www.dockertest.com.conf
	if [ "$2" != "" ]; then
		echo "" >> $1/conf.d/www.dockertest.com.conf
		echo "    include conf.d/$2;" >> $1/conf.d/www.dockertest.com.conf
	fi
	echo "}" >> $1/conf.d/www.dockertest.com.conf
}

# 初始化php配置信息
function write_php_data(){
	echo "extension=bcmath.so" >> $1/phpini/extensions.ini
	echo "extension=pdo_mysql.so" >> $1/phpini/extensions.ini
	echo "extension=bz2.so" >> $1/phpini/extensions.ini
	echo "extension=shmop.so" >> $1/phpini/extensions.ini
	echo "extension=gd.so" >> $1/phpini/extensions.ini
	echo "extension=sockets.so" >> $1/phpini/extensions.ini
	echo "extension=gettext.so" >> $1/phpini/extensions.ini
	echo "extension=sodium.so" >> $1/phpini/extensions.ini
	echo "extension=mysqli.so" >> $1/phpini/extensions.ini
	echo "extension=zip.so" >> $1/phpini/extensions.ini
	echo "extension=swoole.so" >> $1/phpini/extensions.ini
	echo "extension=redis.so" >> $1/phpini/extensions.ini
}

# 初始化mysql配置信息
function write_mysql_data(){
	# docker.cnf
	echo "[mysqld]" >> $1/conf.d/docker.cnf
	echo "skip-host-cache" >> $1/conf.d/docker.cnf
	echo "skip-name-resolve" >> $1/conf.d/docker.cnf

	# mysql.cnf
	echo "[mysql]" >> $1/conf.d/mysql.cnf

	# mysqldump.cnf
	echo "[mysqldump]" >> $1/conf.d/mysqldump.cnf
	echo "quick" >> $1/conf.d/mysqldump.cnf
	echo "quote-names" >> $1/conf.d/mysqldump.cnf
	echo "max_allowed_packet = 16M" >> $1/conf.d/mysqldump.cnf
}


# 判断脚本是否执行过
if [ -d "www" ]; then
	echo '删除www文件夹可重新构建，重新构建将重置所有配置信息，注意备份数据'
  	exit
fi

# 核心流程代码（循环services里面的配置，生成php、mysql、nginx、redis相关的基础文件夹和文件）
init
services=($(get_services docker-compose.yml))

# 循环获取所有的php数据
all_php=()
for php_item in ${services[@]}
do
	if [ "$(echo ${php_item} | grep "phpfpm")" != "" ]; then
		all_php[${#all_php[@]}]=${php_item}
	fi
done

# 循环创建各种服务对应的文件信息
for sc_item in ${services[@]}
do
	if [ "$(echo ${sc_item} | grep "nginx")" != "" ]; then
		# nginx处理
		if [ -d "${sc_item}" ]; then
			rm -rf ${sc_item}
		fi

	  	mkdir -p ${sc_item}/conf.d

	  	# 写入fpm配置
	  	fpm_name=''
	  	for fpm_item in ${all_php[@]}
	  	do
	  		result=$(write_nginx_fpm ${sc_item} ${fpm_item})
	  		if [ "$fpm_name" == "" ]; then
	  			fpm_name=${result}
	  		fi
	  	done

	  	# 写入conf
	  	write_nginx_conf ${sc_item} ${fpm_name}
	elif [ "$(echo ${sc_item} | grep "phpfpm")" != "" ]; then
		# php处理
		if [ -d "${sc_item}" ]; then
			rm -rf ${sc_item}
		fi

	  	mkdir -p ${sc_item}/phpini
	  	write_php_data ${sc_item}
	elif [ "$(echo ${sc_item} | grep "mysql")" != "" ]; then
		# mysql处理
		if [ -d "${sc_item}" ]; then
			rm -rf ${sc_item}
		fi

		mkdir -p ${sc_item}/conf.d
		mkdir -p ${sc_item}/data
		write_mysql_data ${sc_item}
	fi
done

# 生成docker-compose
docker-compose up -d